package com.hankol.newretail

import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.Constants
import com.baselibrary.MessageBus
import com.baselibrary.activity.BaseActivity
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.*
import com.baselibrary.utils.EmptyViewUtil
import com.baselibrary.view.ItemDecoration
import com.hankol.newretail.adapter.AfterSalesAdapter
import com.hankol.newretail.bean.AfterSaleListBean
import com.hankol.newretail.databinding.ActivityAfterSalesBinding
import com.hankol.newretail.viewmodel.AfterSalesViewModel
import com.liaoinstan.springview.utils.DensityUtil
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * @description 售后列表
 * @author 关忆北、
 * @time 2022/8/10 1:50 下午
 */
@Route(path = ActivityRouter.NEW_RETAIL.RETAIL_AFTER_SALES)
class AfterSalesListActivity : BaseActivity() {
    private var pager: Int = 1
    private var clickPos = -1
    private lateinit var viewModel: AfterSalesViewModel
    private lateinit var dataBinding: ActivityAfterSalesBinding
    private val mAfterSalesAdapter: AfterSalesAdapter by lazy { AfterSalesAdapter() }
    override fun getLayoutId(): Int {
        return R.layout.activity_after_sales
    }

    override fun init() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
        dataBinding = getDataBinding<ActivityAfterSalesBinding>()
        viewModel = getActivityViewModelProvider(this).get(AfterSalesViewModel::class.java)
        dataBinding.rvAfterSalesList.adapter = mAfterSalesAdapter
        dataBinding.rvAfterSalesList.addItemDecoration(ItemDecoration(DensityUtil.dip2px(this, 14f), DensityUtil.dip2px(this, 8f)))
        reqData()
        initObserve()
        initListener()

    }

    fun reqData() {
        viewModel.reqAfterSaleList(pager)
        //mAfterSalesAdapter.setNewData(listBean)
    }

    fun initListener() {
        dataBinding.refreshAfterSales.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                pager = 1
                viewModel.reqAfterSaleList(pager)

            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.reqAfterSaleList(pager)
            }
        })
        dataBinding.ivBack.setOnClickListener { finish() }


        mAfterSalesAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (view.id == R.id.tv_after_sales_revoke) {
                PopUtils.showOkCancelDialog(this, "确认撤销申请", object : PopUtils.ICallBack {
                    override fun clickOk() {
                        clickPos = position
                        val afterSaleListBean = adapter.data[position]
                        if (afterSaleListBean is AfterSaleListBean) {
                            viewModel.reqRevokeAfterSale(afterSaleListBean.afterSaleId)
                        }
                    }

                    override fun clickCancel() {}
                }, false, "确认", "取消", true)
            }
        }

        mAfterSalesAdapter.setOnItemClickListener { adapter, view, position ->
            val afterSaleListBean = adapter.data[position]
            if (afterSaleListBean is AfterSaleListBean) {
                clickPos = position
                val hasMap: HashMap<String, Any> = hashMapOf()
                hasMap[AfterSalesDetailsActivity.AFTER_SALES_ID] = afterSaleListBean.afterSaleId
                ARouterUtils.jump(ActivityRouter.NEW_RETAIL.AFTER_SALES_DETAILS, hasMap)
            }
        }
    }

    private fun initObserve() {
        viewModel.revokeAfterSaleLD.observe(this, Observer {
            if (it != null && it.success) {
                ToastUtils.showShort(applicationContext, "撤销成功")
                EventBusUtils.sendNotify(MessageBus.msgId_retail_after_sales_revoke)
            }
        })
        viewModel.afterSaleListLD.observe(this, Observer {
            dataBinding.refreshAfterSales.finishRefresh()
            dataBinding.refreshAfterSales.finishLoadMore()
            if (it.data.isNotEmpty()) {
                if (pager <= 1) {
                    mAfterSalesAdapter.setNewData(it.data)
                } else {
                    mAfterSalesAdapter.addData(it.data)
                }
                pager++
            } else {
                if (pager <= 1) {
                    mAfterSalesAdapter.setNewData(arrayListOf())
                    val emptyView = EmptyViewUtil.getEmptyView(Constants.TYPE_EVALUATION_LIST, dataBinding.rvAfterSalesList, "暂无售后订单")
                    mAfterSalesAdapter.emptyView = emptyView
                }
            }
        })
    }

    //EventBus 接受方法
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEvent(messageBus: MessageBus) {
        if (messageBus.codeType == MessageBus.msgId_retail_after_sales_revoke) {
            mAfterSalesAdapter.remove(clickPos)

            if (mAfterSalesAdapter.data.isNullOrEmpty()) {
                mAfterSalesAdapter.setNewData(arrayListOf())
                val emptyView = EmptyViewUtil.getEmptyView(Constants.TYPE_EVALUATION_LIST, dataBinding.rvAfterSalesList, "暂无售后订单")
                mAfterSalesAdapter.emptyView = emptyView
            }
        } else if (messageBus.codeType == MessageBus.msgId_retail_after_sales_re_apply_success) {
            if (mAfterSalesAdapter.data.isNotEmpty()) {
                pager = 1
                reqData()
//                mAfterSalesAdapter.data[clickPos].afterSaleStatus = 0
//                mAfterSalesAdapter.notifyItemChanged(clickPos)
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
    }
}